#setwd("C:/Users/Jerry/Documents/CSE 446/project/cse446-project")

#calculate model script
source("mean_centered.R")
source("eigenvector.R")
source("k-NN.R")
source("drawVect.R")

##############################
# USEFUL FUNCTIONS
##############################

#projects a vector in the original face space to the eigenspace
project <- function(x,e) {rowSums(x*e)}
##############################
# FILE I/O
##############################

set.seed(142^2)

#find all the relevant files
people <- list.files("./faces")
pictures <- list.files(paste("./faces/",people,sep=""),full.name=TRUE)
desiredSubset <- grep(value=TRUE,"4.pgm",pictures)

#seperate training and testing data
percentTrain <- 0.8 #TODO hardcoded, should be softer maybe?
sampleCount <- floor(percentTrain*length(desiredSubset))
TrainIndices <- sample(1:length(desiredSubset),sampleCount)

TestIndices <- c() #TODO: this is super hacky and should die in a fire
i <- 1
for(k in 1:length(desiredSubset)) {
  if(k %in% TrainIndices) {
    
  } else {
    TestIndices[i] <- k
    i <- i + 1
  }
}

TrainingData <- desiredSubset[TrainIndices]
TestData <- desiredSubset[TestIndices]



mean_info <- get_mean_centered_matrix(TrainingData)

# the mean-centered matrix of all the training images
A <- mean_info[[1]]

# the mean vector of all the training images
trainingMean <- mean_info[[2]]

# the original (non-mean-centered) matrix of all the training images
A_non_centered <- mean_info[[3]]


##############################
# TEST
##############################


#png(file="acc_rate.png", width=800, height=600)
#par(mar=c(6,7,6,6)+0.1)
#plot(kays, recognitionRates[kays], type="b", font.lab=2, pch=19, main = "Eigenface Recognition Accuracy Rate v.s. Number of Eigenvalues Picked", xlab="Number of eigenvalues used to span the face space", ylab="Recog. Accuracy Rate on Test Set", cex.lab=1.5, cex.main=1.8)
#dev.off()

##############################
# FOR FUN
##############################
#k = 10
#e <- getEigenVect(A %*% t(A),k) #get our basis functions
